#ifndef SERVER_H
#define SERVER_H

#include <QApplication>
#include <QDialog>
#include <QMap>
#include <QStack>
#include <QTimer>
#include <QFile>
#include <QTcpServer>
#include <QProgressBar>
#include <QLabel>
#include <QList>
#include <QSortFilterProxyModel>
#include "ui_mainWindow.h"
#include "multicastsocket.h"
#include "clientLogic.h"
#include "data_model.h"
#include "..\commons\commons.h"
#include "..\commons\fileSenderManager.h"
#include "serverXmlCreator.h"
#include "serverXmlReader.h"
#include "serverRspHndlThread.h"


class Server: public QMainWindow, private Ui::MainWindow
{
	Q_OBJECT

public:
	Server(QApplication *,QWidget *parent = 0);
	~Server();
	ClientLogic *getClientLogic(int id);
	void statusChanged();
	void storeLogString(QString &logStr,ClientLogic* cl,QString appName);
	int getClientGroup(int);
	void showWarning(QString);
	void showMessage(QString);
	QTcpSocket* getPendingSocket();
	bool isLightMode() {return lightMode;};
signals:
	void stopThread();
protected:
//	void closeEvent(QCloseEvent *event);
public slots:
	void showLogString();
//	void handleClientResponse();
//	void handleSocketError(QAbstractSocket::SocketError);
private slots:

	void readDiscoveryPacket();
	void checkDisconnected();
	void performRefresh();
	void gestisciInstalla();
	void gestisciRun();
	void gestisciArresta();
	void gestisciUninstall();
	void gestisciScarica();
	void gestisciStato();
	void trasferimentoCompletato();
	void trasferimentoComandoCompletato();
	void trasferimentoErrato();
	void trasferimentoComandoErrato();
	void inviaApplicazione();
//	void showStatus(QList<ClientLogic*> client_list);
//	void getLogs(QList<ClientLogic*> client_list, QStringList applicazioni);
	void setGiro(int completo, int giro);
	void handleSelectionChange( const  QItemSelection & current, const  QItemSelection & previous );
	void handleTableSelectionChange( const  QItemSelection & current, const  QItemSelection & previous );
	void tableContextMenu(const QPoint &pos);
	void handleTableDblClick(const QModelIndex&);
//	void handleCompleted();
	void checkboxStateChanged(int state);
private:
	void createActions();
	void fillAddressFromSelection(QVector<QPair<quint32, quint16>> *indirizzi);
	void enableButtons(bool);
	void initializeGroupMap();
/*
	
	bool uninstall(String applicazione, List client);
	bool run(String applicazione, List client, String args);
	
	
*/
	QApplication *appRef;
	//base dati + grafica
	QMap<int, ClientLogic*> client_list; 
	QMap<int, int> groupMap;
	QMap<int, QList<ClientLogic*>*> group_list;
	DataModel *dm;
	ApplicationTableModel *tdm;
	QItemSelectionModel *selectionModel;
	MulticastSocket discovery_socket;
	QTimer clientTimer;
	MulticastSocket forceDiscoverySock_out;
	//Sono cose temporanee
	//bisogna trovare il modo di gestirle
	//in modo furbo
	FileSenderManager *cmdFsm;
	FileSenderManager *appFsm;
	QFile *appFile;
	QBuffer *buffer;
	QHostAddress localAddress;
	ServerRespHandler *responseHandler;
	QProgressBar *progbar;
	QLabel *proglabel;
	//actions
	QStack<QPair<ClientLogic*,QPair<QString, QString> > > *clStack;
	QAction *runAction;
	QAction *installAction;
	QAction *uninstallAction;
	QAction *getLogsAction;
	QString *logString;
	QAction *actionKill;
	QSortFilterProxyModel *proxyModel;
	QTcpServer *respSocket;
	QMutex *logMutex;
	bool lightMode;
};

#endif
